package handler

import (
	"github.com/zeromicro/go-zero/core/logx"
	"net/http"

	"cloud-disk/core/internal/logic"
	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"
	"github.com/zeromicro/go-zero/rest/httpx"
)

func UserRepositorySaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {

	return func(w http.ResponseWriter, r *http.Request) {
		var req types.UserRepositorySaveRequest
		if err := httpx.Parse(r, &req); err != nil {
			logx.Errorf("解析请求失败: %v", err)
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewUserRepositorySaveLogic(r.Context(), svcCtx)

		// 从请求头或上下文中获取 userIdentity
		userIdentity := r.Header.Get("UserIdentity") // 或者从上下文中获取，取决于您的认证实现

		resp, err := l.UserRepositorySave(&req, userIdentity)
		if err != nil {
			logx.Errorf("UserRepositorySave 失败: %+v", err)
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		httpx.OkJsonCtx(r.Context(), w, resp)
	}
}
